package com.potevio.simulator.strategy.impl;


import com.potevio.simulator.common.CommonFlag;
import com.potevio.simulator.common.GetHexMessage;
import com.potevio.simulator.config.MsgTypeConst;
import com.potevio.simulator.entity.ChargeModelChargeMsg0x39;
import com.potevio.simulator.heart.HeartbeatClient;
import com.potevio.simulator.strategy.ReceiveMsgStrategy;
import com.potevio.simulator.task.ChargeMsgTask;
import com.potevio.simulator.util.HexUtil;
import io.netty.channel.ChannelHandlerContext;
import lombok.extern.slf4j.Slf4j;

import java.math.BigDecimal;
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.Set;

/**
 * @ClassName Receive69
 * @Description: 接收69报文
 * @Date 2021/6/1  10:27
 * @Author zyd
 */
@Slf4j
public class Receive69Command implements ReceiveMsgStrategy {

    @Override
    public void receiveMessage(ChannelHandlerContext ctx, Map<String, LinkedHashMap<String, String>> resultHexMap, String revertCommandCode) throws Exception {
        String chargePort = MsgTypeConst.CHARGE_POSTS.get(MsgTypeConst.CHARGE_POSTS.size()-1);
        if ("3.4a".equals(CommonFlag.version) || "3.4a1".equals(CommonFlag.version)) {
            MsgTypeConst.chm_Map.put("0x69_"+chargePort, resultHexMap.get(revertCommandCode));
        }else{
            MsgTypeConst.chm_Map.put("0x69", resultHexMap.get(revertCommandCode));
        }

        send39Msg();
    }

    /**
     * 0x39报文
     */
    public void send39Msg() {
        try {
            log.info("定时发送充电报文0x39——msgLog");
            Map<String, LinkedHashMap<String, String>> map = new GetHexMessage().getDataDomain(MsgTypeConst.CHARGE_MODEL_CHARGE_MSG);
            LinkedHashMap<String, String> chMap = map.get("0x39");
            Set<String> tempKey = chMap.keySet();
            Iterator<String> iterator1 = tempKey.iterator();
            if (HeartbeatClient.chargingPileState == 1) {
                while (iterator1.hasNext()) {
                    String key2 = iterator1.next();
                    BigDecimal bigDecimal;
                    // 电能
                    if ("cumulativeChargeKwh".equals(key2)) {
                        bigDecimal = HexUtil.makeRandom(3f, 1f, 2);
                        ChargeMsgTask.quantityElectricity = ChargeMsgTask.quantityElectricity.add(bigDecimal);
                        // 定额充电，到了设定电量自动停止
                        int i = ChargeMsgTask.quantityElectricity.multiply(new BigDecimal(100)).intValue();
                        if (i >= HeartbeatClient.ratedCharge * 100) {
                            // 修改充电桩状态
//                            new CommonTask().updateChargeState();
                        }
                        String qeStr = HexUtil.random2HexAndReverse(i);
                        MsgTypeConst.chm_Map.get("0x39").put("cumulativeChargeKwh", qeStr);
                        chMap.put(key2, qeStr);
                        //  电量
                    } else if ("cumulativeChargeAh".equals(key2)) {
                        int temp = HexUtil.makeRandom(3f, 1f, 0).intValue();
                        ChargeMsgTask.electricEnergy = ChargeMsgTask.electricEnergy + temp;
                        String eeStr = HexUtil.random2HexAndReverse(ChargeMsgTask.electricEnergy);
                        MsgTypeConst.chm_Map.get("0x39").put("cumulativeChargeAh", eeStr);
                        chMap.put(key2, eeStr);
                        // 累计充电时间
                    } else if ("cumulativeChargeTime".equals(key2)) {
                        ChargeMsgTask.chargeTime = ChargeMsgTask.chargeTime + HeartbeatClient.chargeMsgInterval;
                        log.info("累计充电时长cumulativeChargeTime=" + ChargeMsgTask.chargeTime);
                        String ctStr = "";
                        if (ChargeMsgTask.chargeTime <= 15) {
                            ctStr = HexUtil.int2Hex(ChargeMsgTask.chargeTime) + "00";
                            log.info("小于15秒chargeTime=" + ctStr);
                        } else {
                            ctStr = HexUtil.random2HexAndReverse(ChargeMsgTask.chargeTime);
                            log.info("大于15秒chargeTime=" + ctStr);
                        }
                        MsgTypeConst.chm_Map.get("0x39").put("cumulativeChargeTime", ctStr);
                        chMap.put(key2, ctStr);
                        // 当前SOC
                    } else if ("currentSOC".equals(key2)) {
                        ChargeMsgTask.currentSOC++;
                        log.info("当前SOC——msgLog：" + ChargeMsgTask.currentSOC);
                        MsgTypeConst.chm_Map.get("0x39").put("chargeStartSOC", ChargeModelChargeMsg0x39.chargeStartSOC);
                        if (ChargeMsgTask.currentSOC >= 100) {
                            ChargeMsgTask.quantityElectricity = new BigDecimal(0);
                            ChargeMsgTask.electricEnergy = 0;
                            ChargeMsgTask.chargeTime = 0;
                            ChargeMsgTask.InitMeterDegrees = 0;
                            ChargeMsgTask.currentSOC = Integer.valueOf(HexUtil.hex2Int(ChargeModelChargeMsg0x39.currentSOC));
                            ChargeMsgTask.send00Number = 1;
                            ChargeMsgTask.stopFlag = false;
                            log.info("停止充电——msgLog。。。。。。。。。。。。。。。");
                        }
                        chMap.put(key2, HexUtil.int2Hex(ChargeMsgTask.currentSOC));
                        // 电表度数Kwh
                    } else if ("ammeterDegrees".equals(key2)) {
                        int meterDegrees = ChargeMsgTask.quantityElectricity.add(new BigDecimal(ChargeMsgTask.InitMeterDegrees)).multiply(new BigDecimal(100)).intValue();
                        log.info("最后一次电表度数：{}", meterDegrees);
                        String mdHexStr = HexUtil.random2HexAndReverse(meterDegrees);
                        mdHexStr = HexUtil.getIntervalByteNum(mdHexStr, "0x39", key2);
                        chMap.put(key2, mdHexStr);
                    }
                }
            }
            map.put("0x39", chMap);
            HeartbeatClient.channel.writeAndFlush(map);
        } catch (Exception e) {
            log.error("0x39报文发送异常：{}", e);
        }
    }

}
